


# 数据对齐功能，基于deal中的csv，实现不同的数据对齐，返回对齐的数据
import pandas as pd
import numpy as np
df=pd.DataFrame()





# 对齐室内跑步的速度，和阶段，ecg和rr，暂时没想好如何实现


#对齐后的数据可用于对比rr
def ecgrr_ppgrr(rrdata_df,rri_df,start_time,end_time):

    '''

    :param ecg_df: ecg
    :param ppg_df: ppg的rr
    :param start_time: 开始时间
    :param end_time: 结束时间
    :return:
    '''

    df_rri_ppg=rri_df
    df_rr_new=rrdata_df
    # print(df_rri_ppg)
    # print(df_rr_new)
    # 提取前10位时间戳并转换为秒
    df_rri_ppg['timestamp_seconds'] = pd.to_datetime(df_rri_ppg['timestamp'].str[:10], unit='s',
                                                     utc=True).dt.tz_convert('Asia/Shanghai')
    df_rr_new['timestamp_seconds'] = pd.to_datetime(df_rr_new['timestamp'].str[:10], unit='s', utc=True).dt.tz_convert(
        'Asia/Shanghai')

    # 指定开始和结束时间范围（精确到秒）


    # 选择指定时间范围内的数据
    df_rri_ppg_selected = df_rri_ppg[
        (df_rri_ppg['timestamp_seconds'] >= start_time) & (df_rri_ppg['timestamp_seconds'] <= end_time) & (df_rri_ppg['sqi']=='100')]

    df_rr_new_selected = df_rr_new[
        (df_rr_new['timestamp_seconds'] >= start_time) & (df_rr_new['timestamp_seconds'] <= end_time)]
    df_rri_ppg_selected.sort_values(by='timestamp_seconds',inplace=True)
    df_rr_new_selected.sort_values(by='timestamp_seconds', inplace=True)
    # print(df_rr_new_selected)
    # print(df_rri_ppg_selected)


    df=pd.concat([df_rri_ppg_selected.reset_index(drop=True),df_rr_new_selected.reset_index(drop=True)],axis=1,keys=['ppg', 'ecg'])
    print(df)
    return df


# 实现所有数据都对齐，ecg，rr，ppg，ppg的rr，singledetail，singlework，    speed和la之后考虑
def all_align(ecg_df,rrdata_df,ppg_df,rri_df,singlework_df,singledetail_df,person,test):
    # print(ecg_df)
    # print(rrdata_df)

    ecg_df['ecg_timestamp']=ecg_df['ecg_timestamp'].astype(np.int64)
    rrdata_df['timestamp'] = rrdata_df['timestamp'].astype(np.int64)
    result = pd.merge(ecg_df, rrdata_df,  left_on='ecg_timestamp', right_on='timestamp', how='left')
    print(result)
    ppg_df['PPG_TIME']=ppg_df['PPG_TIME'].astype(np.int64)
    ppg_df['PPG_TIME'] = ppg_df['PPG_TIME'] + ppg_df.groupby('PPG_TIME').cumcount()*10
    result['ecg_timestamp'] = result['ecg_timestamp'].astype(str).str[:12]
    ppg_df['PPG_TIME'] = ppg_df['PPG_TIME'].astype(str).str[:12]
    result = pd.merge(result, ppg_df, left_on='ecg_timestamp', right_on='PPG_TIME', how='left')
    # print(result)
    rri_df=rri_df[rri_df['sqi']=='100']
    rri_df.loc[:,'timestamp'] = rri_df['timestamp'].astype(str).str[:12]
    result = pd.merge(result, rri_df, left_on='ecg_timestamp', right_on='timestamp', how='left')
    result['ecg_timestamp_10']=result['ecg_timestamp'].astype(str).str[:10]
    singledetail_df['timestamp']=singledetail_df['timestamp'].astype(str).str[:10]
    # print(singledetail_df)
    result = pd.merge(result, singledetail_df, left_on='ecg_timestamp_10', right_on='timestamp', how='left')
    result['name'] = ''
    singlework_df['name']=["rest","warmup","running","stand","run_rest"]
    print(result)
    for index, row in singlework_df.iterrows():
        condition=(result['数据时间']>=row['活动.测量开始时间'])&(result['数据时间']<=row['活动.测量结束时间'])
        result.loc[condition, 'name'] = row['name']
        print(row['name'],row['活动.测量开始时间'],row['活动.测量结束时间'])
    print(singlework_df)
    result['test']=test


    # result.to_csv('output.csv',index=False)
    return result


